import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  Unique,
} from 'typeorm';

@Entity('users')
@Unique(['nickname'])
export class Users {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({ type: 'varchar', length: 50, comment: '姓名' })
  name!: string;

  @Column({
    type: 'smallint',
    nullable: true,
    comment: '性别（0-未知，1-男，2-女，3-其他）',
  })
  gender?: number;

  @Column({
    type: 'smallint',
    nullable: true,
    comment: '身份标识（0-用户，1-管理员）',
  })
  role?: number;

  @Column({
    type: 'smallint',
    nullable: true,
    comment: '年龄',
  })
  age?: number;

  @Column({
    type: 'varchar',
    length: 50,
    nullable: true,
    comment: '昵称',
  })
  nickname?: string;

  // 必填项：使用确定赋值断言，告诉编译器运行时会有值
  @Column({
    type: 'varchar',
    length: 255,
    comment: '密码（建议存储加密后的值）',
  })
  password!: string;

  @Column({
    name: 'profile_code',
    type: 'varchar',
    length: 255,
    nullable: true,
    comment: '个人码（图片路径或唯一标识）',
  })
  profileCode?: string;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    comment: '头像（图片路径或URL）',
  })
  head?: string;

  @CreateDateColumn({ name: 'created_at', type: 'datetime', comment: '创建时间', nullable: true })
  createdAt?: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'datetime', comment: '更新时间', nullable: true })
  updatedAt?: Date;

  // 可选的便捷构造器，方便用对象直接 new Users({ name, password, ... })
  constructor(partial?: Partial<Users>) {
    if (partial) {
      Object.assign(this, partial);
    }
  }
}
